//
//  ViewController.m
//  04UI-HomeWork
//
//  Created by 冀永金 on 16/7/19.
//  Copyright © 2016年 冀永金. All rights reserved.
//

#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>
#import "MyImageView.h"
#import "MyLabel.h"
#import "MyButton.h"
#import "LrcParser.h"
#define WIDTH self.view.frame.size.width
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UIImageView *backImageView;
@property (nonatomic,  strong) AVAudioPlayer *player;
@property (nonatomic, strong) NSTimer *timer;
@property (nonatomic, assign) CGFloat f;
@property (nonatomic, strong) UIImageView *imageOfBig;
@property (nonatomic, strong) UITableView *lrcTable;
@property (nonatomic, strong) LrcParser *lrcContent;
@property (nonatomic, assign) NSInteger currentRow;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    NSString *path = [[NSBundle mainBundle] pathForResource:@"稻香" ofType:@".mp3"];
    NSURL *url = [NSURL fileURLWithPath:path];
    self.player = [[AVAudioPlayer alloc]initWithContentsOfURL:url error:nil];
    NSLog(@"%f",self.player.duration);
   self.backImageView = [[UIImageView alloc]initWithFrame:self.view.bounds];
    [self.view addSubview:_backImageView];
    _backImageView.image = [UIImage imageNamed:@"profile_homepage_default_bg"];
    [self config];
    [self createTableView];
    
       // Do any additional setup after loading the view, typically from a nib.
}
- (void)config {
    MyButton *downButton = [MyButton buttonWithFrame:CGRectMake(20, 20, 50, 50) image:[UIImage imageNamed:@"1"]];
    [self.view addSubview:downButton];
    MyButton *moreButton = [MyButton buttonWithFrame:CGRectMake(WIDTH - 70, 20, 50, 50) image:[UIImage imageNamed:@"Unknown 下午3.29.58"]];
    [self.view addSubview:moreButton];
    MyLabel *labelOfDaoXiang = [MyLabel labelWithFrame:CGRectMake(0, 0, 80, 40) text:@"稻香" tintColor:[UIColor whiteColor] backGroundColor:nil font:22];
    [self.backImageView addSubview:labelOfDaoXiang];
    labelOfDaoXiang.textAlignment = 1;
    labelOfDaoXiang.center = CGPointMake(WIDTH / 2, 40);
    MyLabel *labelOfName = [MyLabel labelWithFrame:CGRectMake(0, 0, 180, 40) text:@"—— 周杰伦 ——" tintColor:[UIColor whiteColor] backGroundColor:nil font:20];
    [self.backImageView addSubview:labelOfName];
    labelOfName.center = CGPointMake(WIDTH / 2, 80);
    labelOfName.textAlignment = 1;
    MyImageView *imageOfStandard = [MyImageView imageWithFrame:CGRectMake(80, 120, 60, 30) image:[UIImage imageNamed:@"player_btn_bz_sel_normal"]];
    [self.backImageView addSubview:imageOfStandard];
    MyImageView *imageOfOnly = [MyImageView imageWithFrame:CGRectMake(150, 120, 60, 30) image:[UIImage imageNamed:@"player_btn_copyright_normal"]];
    [self.backImageView addSubview:imageOfOnly];
    MyImageView *imageOfMV = [MyImageView imageWithFrame:CGRectMake(220, 120, 60, 30) image:[UIImage imageNamed:@"cell_mv_icon"]];
    [self.backImageView addSubview:imageOfMV];
    MyImageView *imageOfLogo = [MyImageView imageWithFrame:CGRectMake(290, 120, 60, 30) image:[UIImage imageNamed:@"player_btn_dts_on"]];
    [self.backImageView addSubview:imageOfLogo];
    //背景的View
    UIView *backView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 320)];
    [self.backImageView addSubview:backView];
    backView.center = CGPointMake(WIDTH / 2, 330);
    backView.layer.cornerRadius = 160;
    backView.clipsToBounds = YES;
    backView.backgroundColor = [UIColor colorWithRed:0.16 green:0.11 blue:0.07 alpha:1.00];
    backView.alpha = .7;
    //旋转大图片
  self.imageOfBig = [MyImageView imageWithFrame:CGRectMake(0, 0, 300, 300) image:[UIImage imageNamed:@"7c1ed21b0ef41bd5b1fd68dd55da81cb39db3d68"]];
    [self.view addSubview:_imageOfBig];
    _imageOfBig.center = CGPointMake(WIDTH / 2, 330);
    _imageOfBig.layer.cornerRadius = 150;
    _imageOfBig.clipsToBounds = YES;
    self.f = 1;
    //转动第三种方法初始化动画
    [self initAnimation];
    // 先不让动画动
    [self pauseLayer:self.imageOfBig.layer];
    
    
    MyLabel *labelOfSong = [MyLabel labelWithFrame:CGRectMake(0, 0, 160, 40) text:@"曲: 周杰伦" tintColor:[UIColor whiteColor] backGroundColor:nil font:20];
    labelOfSong.center = CGPointMake(WIDTH / 2, 520);
    labelOfSong.textAlignment = 1;
    [self.backImageView addSubview:labelOfSong];
    UIPageControl *page = [[UIPageControl alloc]initWithFrame:CGRectMake(0, 0, 80, 20)];
    [self.view addSubview:page];
    page.center = CGPointMake(WIDTH / 2, 550);
    page.numberOfPages = 3;
    //时间
    NSString *time =[NSString stringWithFormat:@"%02d:%02d",(int) self.player.currentTime / 60,(int) self.player.currentTime % 60];
    NSLog(@"%f",self.player.currentTime);
    MyLabel *leftLabelTime = [[MyLabel alloc]initWithFrame:CGRectMake(20, 570, 50, 20) text:time tintColor:[UIColor whiteColor] backGroundColor:[UIColor colorWithRed:0.00 green:0.11 blue:0.17 alpha:1.00] font:12];
    [self.backImageView addSubview:leftLabelTime];
    UISlider *slider = [[UISlider alloc]initWithFrame:CGRectMake(80, 570, WIDTH - 160, 20)];
    [self.view addSubview:slider];
    slider.minimumTrackTintColor = [UIColor colorWithRed:0.21 green:0.77 blue:0.49 alpha:1.00];
    slider.maximumTrackTintColor = [UIColor colorWithRed:0.43 green:0.39 blue:0.36 alpha:1.00];
    slider.maximumValue = self.player.duration;
    slider.tag = 100;
    [slider addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventValueChanged];
    //定时器 让slider随着player播放移动
    self.timer = [NSTimer scheduledTimerWithTimeInterval:.2 target:self selector:@selector(changeTime) userInfo:nil repeats:YES];
    
    NSString *totalTime = [NSString stringWithFormat:@"%02d:%02d",(int)self.player.duration / 60,(int)self.player.duration % 60];
    
    MyLabel *rightLabelTime = [MyLabel labelWithFrame:CGRectMake(WIDTH - 70, 570, 50, 20) text:totalTime tintColor:[UIColor whiteColor] backGroundColor:[UIColor colorWithRed:0.00 green:0.11 blue:0.17 alpha:1.00] font:12];
    [self.backImageView addSubview:rightLabelTime];
  
    MyButton *lastButton = [MyButton buttonWithFrame:CGRectMake(85, 600, 80, 80) image:[UIImage imageNamed:@"hp_player_btn_pre_normal"]];
    [self.view addSubview:lastButton];
    MyButton *pauseButton = [MyButton buttonWithFrame:CGRectMake(170, 590, 100, 100) image:[UIImage imageNamed:@"hp_player_btn_play_normal"]];
    [self.view addSubview:pauseButton];
    //事件
    [pauseButton addTarget:self action:@selector(handlePlay:) forControlEvents:UIControlEventTouchUpInside];
    
    MyButton *nextButton = [MyButton buttonWithFrame:CGRectMake(275, 600, 80, 80) image:[UIImage imageNamed:@"8A47DDC2B254D5657B14527FF3A970E8"]];
    [self.view addSubview:nextButton];
    MyButton *mvButton = [MyButton buttonWithFrame:CGRectMake(355, 620, 50, 50) image:[UIImage imageNamed:@"2"]];
    [self.view addSubview:mvButton];
    MyButton *heartButton = [MyButton buttonWithFrame:CGRectMake(25, 680, 40, 40) image:[UIImage imageNamed:@"assetimport_shoucang"]];
    [self.view addSubview:heartButton];
    //事件
    [heartButton addTarget:self action:@selector(handleLike:) forControlEvents:UIControlEventTouchUpInside];
    
    MyButton *cycleButton = [[MyButton alloc]initWithFrame:CGRectMake(95, 680, 40, 40) image:[UIImage imageNamed:@"3"]];
    [self.view addSubview:cycleButton];
    MyButton *downLoadButton = [MyButton buttonWithFrame:CGRectMake(175, 680, 40, 40) image:[UIImage imageNamed:@"4"]];
    [self.view addSubview:downLoadButton];
    MyButton *shareButton = [MyButton buttonWithFrame:CGRectMake(245, 680, 40, 40) image:[UIImage imageNamed:@"5"]];
    [self.view addSubview:shareButton];
    MyButton *musicButton = [MyButton buttonWithFrame:CGRectMake(315, 680, 40, 40) image:[UIImage imageNamed:@"6"]];
    [self.view addSubview:musicButton];
}
- (void)handlePlay:(MyButton *)button {
    [self.player play];
    [button setImage:[UIImage imageNamed:@"hp_player_btn_pause_normal"] forState:UIControlStateNormal];
    CAAnimation *animation = [self.imageOfBig.layer animationForKey:@"rotationAnimation"];
    if (animation == nil) {
        [self initAnimation];
    }
    else {
        [self resumeLayer:self.imageOfBig.layer];
    }
    [button removeTarget:self action:@selector(handlePlay:) forControlEvents:UIControlEventTouchUpInside];
    [button addTarget:self action:@selector(handlePause:) forControlEvents:UIControlEventTouchUpInside];
}
- (void)handlePause:(MyButton *)button {
    [self.player stop];
    [button setImage:[UIImage imageNamed:@"hp_player_btn_play_normal"] forState:UIControlStateNormal];
    CAAnimation *animation = [self.imageOfBig.layer animationForKey:@"rotationAnimation"];
    if (animation) {
        //暂停动画
        [self pauseLayer:self.imageOfBig.layer];
    }

    [button removeTarget:self action:@selector(handlePause:) forControlEvents:UIControlEventTouchUpInside];
    [button addTarget:self action:@selector(handlePlay:) forControlEvents:UIControlEventTouchUpInside];
}
- (void)handleLike:(MyButton *)button {
    [button setImage:[UIImage imageNamed:@"concise_icon_favorite_normal"] forState:UIControlStateNormal];
    [button removeTarget:self action:@selector(handleLike:) forControlEvents:UIControlEventTouchUpInside];
    [button addTarget:self action:@selector(handleCancelLike:) forControlEvents:UIControlEventTouchUpInside];
}
- (void)handleCancelLike:(MyButton *)button {
    [button setImage:[UIImage imageNamed:@"assetimport_shoucang"] forState:UIControlStateNormal];
        [button removeTarget:self action:@selector(handleCancelLike:) forControlEvents:UIControlEventTouchUpInside];
    [button addTarget:self action:@selector(handleLike:) forControlEvents:UIControlEventTouchUpInside];
}
- (void)changeTime {
    UISlider *slider = (UISlider *)[self.view viewWithTag:100];
    slider.value = self.player.currentTime;
    
    CGFloat currentTime = self.player.currentTime;
    
    for (int i = 0; i < self.lrcContent.timeArray.count; i++) {
        NSArray *timeArray = [self.lrcContent.timeArray[i] componentsSeparatedByString:@":"];
        float lrcTime = [timeArray[0] intValue]*60 + [timeArray[1] floatValue];
        if (currentTime > lrcTime) {
            self.currentRow = i;
            
        } else
            break;
    }
    
    [self.lrcTable reloadData];
    [self.lrcTable scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:self.currentRow inSection:0] atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
    
    
    
}
- (void)sliderAction:(UISlider *)slider {
    self.player.currentTime = slider.value;
   
}


- (void)initAnimation {
    // 创建一个绕z轴选择的动画
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    //旋转一周
    animation.toValue = @(2 * M_PI);
    //旋转次数  尽量最大
    animation.repeatCount = MAXFLOAT;
    //执行一次动画需要的时间
    animation.duration = 50.0f;
    [self.imageOfBig.layer addAnimation:animation forKey:@"rotationAnimation"];
    
}

- (void)pauseLayer:(CALayer *)layer {
    CFTimeInterval pauseTime = [layer convertTime:CACurrentMediaTime() fromLayer:nil];
    layer.speed = 0.0;
    layer.timeOffset = pauseTime;
}
- (void)resumeLayer:(CALayer *)layer  {
    CFTimeInterval pauseTime = [layer timeOffset];
    layer.speed = 1.0;
    layer.timeOffset = 0.0;
    layer.beginTime = 0.0;
    CFTimeInterval timeSincePause = [layer convertTime:CACurrentMediaTime() fromLayer:nil] - pauseTime;
    layer.beginTime = timeSincePause;
}
- (void)createTableView {
    self.lrcTable = [[UITableView alloc]initWithFrame:CGRectMake(0, 330, self.view.frame.size.width, 250) style:UITableViewStylePlain];
    [self.view addSubview:self.lrcTable];
    self.lrcTable.backgroundColor = [UIColor clearColor];
    self.lrcTable.delegate = self;
    self.lrcTable.dataSource = self;
    [self.lrcTable registerClass:[UITableViewCell class] forCellReuseIdentifier:@"pool"];
    self.lrcTable.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.lrcContent = [[LrcParser alloc]init];
    [self.lrcContent parseLrc];
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.lrcContent.wordArrary.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"pool"];
    cell.textLabel.text = self.lrcContent.wordArrary[indexPath.row];
    if (indexPath.row == self.currentRow) {
        cell.textLabel.textColor = [UIColor greenColor];
        cell.textLabel.font = [UIFont systemFontOfSize:18];
    } else {
        cell.textLabel.textColor = [UIColor whiteColor];
        cell.textLabel.font = [UIFont systemFontOfSize:15];
    }
    
    cell.backgroundColor = [UIColor clearColor];
    cell.textLabel.textAlignment = 1;
   
    
    return cell;
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
